using System;
using System.Windows.Forms;
using OFX.Aggregator.Infrastructure;

namespace OFX.Aggregator.Implementation.BESnet.Clipboard.CreditCard
{
	public partial class PasteStatementUserControl : UserControl
	{
		private readonly IStatementAction statementAction;
		private readonly StatementSource statementSource;
		private readonly IStatementProcessingWorkflow workflow;

		public PasteStatementUserControl(
			StatementSource source, IStatementAction action,
			IStatementProcessingWorkflow workflow)
		{
			this.statementSource = source;
			this.statementAction = action;
			this.workflow = workflow;

			this.InitializeComponent();
		}

		private void UpdateFormState()
		{
			this.buttonPaste.Enabled = this.statementSource.Retrieval.CanRetrieveStatement();
		}

		private void buttonPaste_Click(object sender, EventArgs e)
		{
			this.workflow.PerformWorkflow(this.statementSource, this.statementAction);
		}

		private void PasteBESnetCreditCardStatementUserControl_ParentChanged(object sender, EventArgs e)
		{
			this.UpdateFormState();

			if (ParentForm != null)
			{
				ParentForm.Activated += this.ParentForm_Activated;
			}
		}

		private void ParentForm_Activated(object sender, EventArgs e)
		{
			this.UpdateFormState();
		}
	}
}